﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace QuanLyThuVien.DAO
{
    class TacGiaDAO
    {
        public static void Them(TacGia TG)
        {
            NorthwindDataContext context = new NorthwindDataContext();
            TacGia a = new TacGia();
            a.TenTacGia = TG.TenTacGia;
            context.TacGias.InsertOnSubmit(a);
            context.SubmitChanges();
        }


        public static DataGridView GetMaTacGia(string TenTacGia)
        {
            NorthwindDataContext db = new NorthwindDataContext();
            DataGridView dt = new DataGridView();
            var sql = from TG in db.TacGias where TG.TenTacGia == TenTacGia select TG;
            dt.DataSource = sql;            
            return dt;
        }

        public static DataGridView GetTenTacGia(int MaTacGia)
        {
            NorthwindDataContext db = new NorthwindDataContext();
            DataGridView dt = new DataGridView();
            var sql = from TG in db.TacGias where TG.MaTacGia == MaTacGia select TG;
            dt.DataSource = sql;
            return dt;
        }

        public static DataGridView GetAllTacGia()
        {
            NorthwindDataContext db = new NorthwindDataContext();
            DataGridView dt = new DataGridView();
            var sql = from TG in db.TacGias select TG;
            dt.DataSource = sql;
            return dt;
        }

        public static void Sua(TacGia S)
        {

            NorthwindDataContext context = new NorthwindDataContext();
            TacGia a = context.TacGias.Single(stgia => stgia.MaTacGia == S.MaTacGia);
            a.TenTacGia = S.TenTacGia;
            context.SubmitChanges();
        }

        //kiem tra xem tac gia co chua
        public static int KT_TacGia(string ten)
        {
            NorthwindDataContext db = new NorthwindDataContext();
            var ds = from tg in db.TacGias
                     where tg.TenTacGia == ten
                     select tg;
            if (ds.Count() > 0)
                return 1;
            return 0;
        }


        //xoa 1 tac gia
        public static void Xoa_TacGia(int matg)
        {
            NorthwindDataContext db = new NorthwindDataContext();
            var tGia = db.TacGias.Single(tacGia => tacGia.MaTacGia == matg);
            db.TacGias.DeleteOnSubmit(tGia);
            db.SubmitChanges();

        }
    }
}
